package com.ruoyi.know.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.know.domain.Notes;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.know.domain.UserNoteFavorites;
import com.ruoyi.know.service.IUserNoteFavoritesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户笔记收藏Controller
 *
 * @author ruoyi
 * @date 2025-02-11
 */
@RestController
@RequestMapping("/know/favorites")
public class UserNoteFavoritesController extends BaseController
{
    @Autowired
    private IUserNoteFavoritesService userNoteFavoritesService;

    /**
     * 查询用户笔记收藏列表
     */
    @PreAuthorize("@ss.hasPermi('know:favorites:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserNoteFavorites userNoteFavorites)
    {
        startPage();
        List<UserNoteFavorites> list = userNoteFavoritesService.selectUserNoteFavoritesList(userNoteFavorites);
        return getDataTable(list);
    }

    /**
     * 导出用户笔记收藏列表
     */
    @PreAuthorize("@ss.hasPermi('know:favorites:export')")
    @Log(title = "用户笔记收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserNoteFavorites userNoteFavorites)
    {
        List<UserNoteFavorites> list = userNoteFavoritesService.selectUserNoteFavoritesList(userNoteFavorites);
        ExcelUtil<UserNoteFavorites> util = new ExcelUtil<UserNoteFavorites>(UserNoteFavorites.class);
        util.exportExcel(response, list, "用户笔记收藏数据");
    }

    /**
     * 获取用户笔记收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('know:favorites:query')")
    @GetMapping(value = "/{favoriteId}")
    public AjaxResult getInfo(@PathVariable("favoriteId") Long favoriteId)
    {
        return success(userNoteFavoritesService.selectUserNoteFavoritesByFavoriteId(favoriteId));
    }

    /**
     * 新增用户笔记收藏
     */
    @PreAuthorize("@ss.hasPermi('know:favorites:add')")
    @Log(title = "用户笔记收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserNoteFavorites userNoteFavorites)
    {
        userNoteFavorites.setUserId(getLoginUser().getUserId());
        userNoteFavorites.setCreateAt(new Date());
        return toAjax(userNoteFavoritesService.insertUserNoteFavorites(userNoteFavorites));
    }

    /**
     * 修改用户笔记收藏
     */
    @PreAuthorize("@ss.hasPermi('know:favorites:edit')")
    @Log(title = "用户笔记收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserNoteFavorites userNoteFavorites)
    {
        return toAjax(userNoteFavoritesService.updateUserNoteFavorites(userNoteFavorites));
    }

    /**
     * 删除用户笔记收藏
     */
    @PreAuthorize("@ss.hasPermi('know:favorites:remove')")
    @Log(title = "用户笔记收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{favoriteIds}")
    public AjaxResult remove(@PathVariable Long[] favoriteIds)
    {
        return toAjax(userNoteFavoritesService.deleteUserNoteFavoritesByFavoriteIds(favoriteIds));
    }

    /**
     * 取消用户笔记收藏
     */
    @PreAuthorize("@ss.hasPermi('know:favorites:cancel')")
    @Log(title = "取消用户笔记收藏", businessType = BusinessType.DELETE)
    @PostMapping("/cancelFavorite")
    public AjaxResult cancel(@RequestBody Notes notes)
    {
        return toAjax(userNoteFavoritesService.cancelUserNoteFavoritesByFavoriteIds(notes));
    }
}
